package com.zh.web;

import com.github.pagehelper.PageInfo;
import com.zh.po.Order;
import com.zh.po.Product;
import com.zh.po.User;
import com.zh.service.OrderService;
import com.zh.service.ProductService;
import com.zh.service.UserProductService;
import com.zh.service.UserService;
import com.zh.web.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserProductService userProductService;


    @GetMapping("/page/{pageNum}/{pageSize}")
    public ResponseVO<PageInfo<Order>> page(@PathVariable Integer pageNum, @PathVariable Integer pageSize){
        PageInfo<Order> page = orderService.page(pageNum, pageSize);
        if (page ==null){
            return new ResponseVO<>(null,"查询失败",4000);
        }
        return new ResponseVO<>(page,"查询成功",2000);
    }


    @PutMapping("/cancelOrder/{orderId}")
    public ResponseVO cancelOrder(@PathVariable Integer orderId){
        int row = orderService.cancelOrder(orderId);
        if (row>0){
            Order orderById = orderService.findOrderById(orderId);
            Product productById = productService.getProductById(orderById.getOrderProduct());
            productById.setProductNum(productById.getProductNum()+orderById.getOrderProductNum());
            productById.setProductState(1);
            productService.updateProduct(productById);
            return new ResponseVO<>(row,"取消订单成功",2000);
        }else {
            return new ResponseVO<>(null,"取消订单失败",4000);
        }
    }


    @PutMapping("/confirmOrder/{orderId}")
    public ResponseVO confirmOrder(@PathVariable Integer orderId){
        int row = orderService.confirmOrder(orderId);
        if (row>0){
            return new ResponseVO<>(row,"发货成功",2000);
        }else {
            return new ResponseVO<>(null,"发货失败",4000);
        }
    }


    @PutMapping("/confirmReceipt/{orderId}")
    public ResponseVO confirmReceipt(@PathVariable Integer orderId){
        int row = orderService.confirmReceipt(orderId);
        if (row>0){
            return new ResponseVO<>(row,"确认收货成功",2000);
        }else {
            return new ResponseVO<>(null,"确认收货失败",4000);
        }
    }


    @PutMapping("/refund/{orderId}")
    public ResponseVO refund(@PathVariable Integer orderId){
        int row = orderService.refund(orderId);
        if (row>0){
            return new ResponseVO<>(row,"申请成功",2000);
        }else {
            return new ResponseVO<>(null,"申请失败",4000);
        }
    }


    @PutMapping("/accessRefund/{orderId}")
    public ResponseVO accessRefund(@PathVariable Integer orderId){
        int row = orderService.accessRefund(orderId);
        if (row>0){
            Order orderById = orderService.findOrderById(orderId);
            // 商品数量回调
            Product productById = productService.getProductById(orderById.getOrderProduct());
            productById.setProductNum(productById.getProductNum()+orderById.getOrderProductNum());
            productById.setProductState(1);
            productService.updateProduct(productById);
            // 金钱回调
            User userByUserId = userService.getUserByUserId(orderById.getOrderUser());
            userByUserId.setUserMoney(userByUserId.getUserMoney()+orderById.getOrderPrice());
            userService.updateUser(userByUserId);
            return new ResponseVO<>(row,"退款成功",2000);
        }else {
            return new ResponseVO<>(null,"退款失败",4000);
        }
    }


    @PutMapping("/backRefund/{orderId}")
    public ResponseVO backRefund(@PathVariable Integer orderId){
        int row = orderService.backRefund(orderId);
        if (row>0){
            return new ResponseVO<>(row,"驳回申请成功",2000);
        }else {
            return new ResponseVO<>(null,"驳回申请失败",4000);
        }
    }


    @PutMapping("/cancelRefund/{orderId}")
    public ResponseVO cancelRefund(@PathVariable Integer orderId){
        int row = orderService.backRefund(orderId);
        if (row>0){
            return new ResponseVO<>(row,"取消申请成功",2000);
        }else {
            return new ResponseVO<>(null,"取消申请失败",4000);
        }
    }


    @PutMapping("/backMoney/{orderId}")
    public ResponseVO backMoney(@PathVariable Integer orderId){
        int row = orderService.backMoney(orderId);
        if (row>0){
            Order orderById = orderService.findOrderById(orderId);
            // 金钱回调
            User userByUserId = userService.getUserByUserId(orderById.getOrderUser());
            userByUserId.setUserMoney(userByUserId.getUserMoney()+orderById.getOrderPrice());
            userService.updateUser(userByUserId);
            // 商品数量回调
            Product productById = productService.getProductById(orderById.getOrderProduct());
            productById.setProductNum(productById.getProductNum()+orderById.getOrderProductNum());
            productById.setProductState(1);
            productService.updateProduct(productById);
            return new ResponseVO<>(row,"退款成功",2000);
        }else {
            return new ResponseVO<>(null,"退款失败",4000);
        }
    }


    @PutMapping("/pay/{orderId}")
    public ResponseVO pay(@PathVariable Integer orderId){
            Order orderById = orderService.findOrderById(orderId);
            User userByUserId = userService.getUserByUserId(orderById.getOrderUser());
            Double orderPrice = orderById.getOrderPrice();// 订单金额
            Double userMoney = userByUserId.getUserMoney();// 现有金额
            // 比较现有金额是否够用
            if (userMoney>=orderPrice){
                // 扣除现有金额
                userByUserId.setUserMoney(userMoney-orderPrice);
                userService.updateUser(userByUserId);
                orderService.pay(orderId);
                return new ResponseVO<>(null,"付款成功",2000);
            }else {
                return new ResponseVO<>(null,"付款失败，余额不足",4000);
            }

    }

    /**
     * @description:  新增订单
     * @param: order 订单
     * @return: com.zh.web.vo.ResponseVO
     */
    @PostMapping
    public ResponseVO addOrder(@RequestBody Order order){
        if (order==null){
            return new ResponseVO<>(null,"新增订单失败",4000);
        }
        order.setOrderPrice(order.getOrderProductNum()*order.getProduct().getProductPrice());
        order.setOrderState(4);// 代付款状态
        int row = orderService.addOrder(order);
        if (row >0 ){
            Integer orderProduct = order.getOrderProduct();// 获取订单商品id
            Product productById = productService.getProductById(orderProduct);// 获取订单商品
            if(productById == null){
                return new ResponseVO<>(null,"新增订单失败",4000);
            }
            productById.setProductNum(productById.getProductNum()-order.getOrderProductNum());
            if (productById.getProductNum()==0){// 商品库存数为0时自动下架商品
                productById.setProductState(0);
            }
            productService.updateProduct(productById);
            userProductService.deleteShoppingCart(orderProduct,order.getOrderUser());
            return new ResponseVO<>(row,"新增订单成功",2000);
        }
        return new ResponseVO<>(null,"新增订单失败",4000);
    }


    @GetMapping("/getOrderById/{orderId}")
    public ResponseVO<Order> getOrderById(@PathVariable Integer orderId){
        Order orderById = orderService.getOrderById(orderId);
        if (orderById==null){
            return new ResponseVO<>(null,"查询失败",4000);
        }
        return new ResponseVO<>(orderById,"查询成功",2000);
    }


    @PutMapping("/updateOrder")
    public ResponseVO updateAddress(@RequestBody Order order){
        System.out.println(order.toString());
        int row = orderService.updateAddress(order);
        if (row>0){
            return new ResponseVO<>(row,"修改成功",2000);
        }else {
            return new ResponseVO<>(null,"修改失败",4000);
        }
    }
}
